home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / make-367.lha / make-3.67 / signame.c < prev    next >
C/C++ Source or Header  |  1993-02-21  |  7KB  |  269 lines

  1. /* Convert between signal names and numbers.
  2.    Copyright (C) 1990, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <stdio.h>
  19. #include <sys/types.h>        /* Some systems need this for <signal.h>.  */
  20. #include <signal.h>
  21.  
  22. #ifdef    HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25.  
  26. /* Some systems do not define NSIG in <signal.h>.  */
  27. #ifndef    NSIG
  28. #ifdef    _NSIG
  29. #define    NSIG    _NSIG
  30. #else
  31. #define    NSIG    32
  32. #endif
  33. #endif
  34.  
  35. #if !__STDC__
  36. #define const
  37. #endif
  38.  
  39. #include "signame.h"
  40.  
  41. #ifndef HAVE_SYS_SIGLIST
  42. /* There is too much variation in Sys V signal numbers and names, so
  43.    we must initialize them at runtime.  */
  44.  
  45. static const char undoc[] = "unknown signal";
  46.  
  47. const char *sys_siglist[NSIG];
  48. #endif /* !HAVE_SYS_SIGLIST */
  49.  
  50. /* Table of abbreviations for signals.  Note:  A given number can
  51.    appear more than once with different abbreviations.  */
  52. typedef struct
  53.   {
  54.     int number;
  55.     const char *abbrev;
  56.   } num_abbrev;
  57. static num_abbrev sig_table[NSIG*2];
  58. /* Number of elements of sig_table used.  */
  59. static int sig_table_nelts = 0;
  60.  
  61. /* Enter signal number NUMBER into the tables with ABBREV and NAME.  */
  62.  
  63. static void
  64. init_sig (number, abbrev, name)
  65.      int number;
  66.      const char *abbrev;
  67.      const char *name;
  68. {
  69. #ifndef HAVE_SYS_SIGLIST
  70.   sys_siglist[number] = name;
  71. #endif
  72.   sig_table[sig_table_nelts].number = number;
  73.   sig_table[sig_table_nelts++].abbrev = abbrev;
  74. }
  75.  
  76. void
  77. signame_init ()
  78. {
  79. #ifndef HAVE_SYS_SIGLIST
  80.   int i;
  81.   /* Initialize signal names.  */
  82.   for (i = 0; i < NSIG; i++)
  83.     sys_siglist[i] = undoc;
  84. #endif /* !HAVE_SYS_SIGLIST */
  85.  
  86.   /* Initialize signal names.  */
  87. #if defined (SIGHUP)
  88.   init_sig (SIGHUP, "HUP", "Hangup");
  89. #endif
  90. #if defined (SIGINT)
  91.   init_sig (SIGINT, "INT", "Interrupt");
  92. #endif
  93. #if defined (SIGQUIT)
  94.   init_sig (SIGQUIT, "QUIT", "Quit");
  95. #endif
  96. #if defined (SIGILL)
  97.   init_sig (SIGILL, "ILL", "Illegal Instruction");
  98. #endif
  99. #if defined (SIGTRAP)
  100.   init_sig (SIGTRAP, "TRAP", "Trace/breakpoint trap");
  101. #endif
  102.   /* If SIGIOT == SIGABRT, we want to print it as SIGABRT because
  103.      SIGABRT is in ANSI and POSIX.1 and SIGIOT isn't.  */
  104. #if defined (SIGABRT)
  105.   init_sig (SIGABRT, "ABRT", "Aborted");
  106. #endif
  107. #if defined (SIGIOT)
  108.   init_sig (SIGIOT, "IOT", "IOT trap");
  109. #endif
  110. #if defined (SIGEMT)
  111.   init_sig (SIGEMT, "EMT", "EMT trap");
  112. #endif
  113. #if defined (SIGFPE)
  114.   init_sig (SIGFPE, "FPE", "Floating point exception");
  115. #endif
  116. #if defined (SIGKILL)
  117.   init_sig (SIGKILL, "KILL", "Killed");
  118. #endif
  119. #if defined (SIGBUS)
  120.   init_sig (SIGBUS, "BUS", "Bus error");
  121. #endif
  122. #if defined (SIGSEGV)
  123.   init_sig (SIGSEGV, "SEGV", "Segmentation fault");
  124. #endif
  125. #if defined (SIGSYS)
  126.   init_sig (SIGSYS, "SYS", "Bad system call");
  127. #endif
  128. #if defined (SIGPIPE)
  129.   init_sig (SIGPIPE, "PIPE", "Broken pipe");
  130. #endif
  131. #if defined (SIGALRM)
  132.   init_sig (SIGALRM, "ALRM", "Alarm clock");
  133. #endif
  134. #if defined (SIGTERM)
  135.   init_sig (SIGTERM, "TERM", "Terminated");
  136. #endif
  137. #if defined (SIGUSR1)
  138.   init_sig (SIGUSR1, "USR1", "User defined signal 1");
  139. #endif
  140. #if defined (SIGUSR2)
  141.   init_sig (SIGUSR2, "USR2", "User defined signal 2");
  142. #endif
  143.   /* If SIGCLD == SIGCHLD, we want to print it as SIGCHLD because that
  144.      is what is in POSIX.1.  */
  145. #if defined (SIGCHLD)
  146.   init_sig (SIGCHLD, "CHLD", "Child exited");
  147. #endif
  148. #if defined (SIGCLD)
  149.   init_sig (SIGCLD, "CLD", "Child exited");
  150. #endif
  151. #if defined (SIGPWR)
  152.   init_sig (SIGPWR, "PWR", "Power failure");
  153. #endif
  154. #if defined (SIGTSTP)
  155.   init_sig (SIGTSTP, "TSTP", "Stopped");
  156. #endif
  157. #if defined (SIGTTIN)
  158.   init_sig (SIGTTIN, "TTIN", "Stopped (tty input)");
  159. #endif
  160. #if defined (SIGTTOU)
  161.   init_sig (SIGTTOU, "TTOU", "Stopped (tty output)");
  162. #endif
  163. #if defined (SIGSTOP)
  164.   init_sig (SIGSTOP, "STOP", "Stopped (signal)");
  165. #endif
  166. #if defined (SIGXCPU)
  167.   init_sig (SIGXCPU, "XCPU", "CPU time limit exceeded");
  168. #endif
  169. #if defined (SIGXFSZ)
  170.   init_sig (SIGXFSZ, "XFSZ", "File size limit exceeded");
  171. #endif
  172. #if defined (SIGVTALRM)
  173.   init_sig (SIGVTALRM, "VTALRM", "Virtual timer expired");
  174. #endif
  175. #if defined (SIGPROF)
  176.   init_sig (SIGPROF, "PROF", "Profiling timer expired");
  177. #endif
  178. #if defined (SIGWINCH)
  179.   /* "Window size changed" might be more accurate, but even if that
  180.      is all that it means now, perhaps in the future it will be
  181.      extended to cover other kinds of window changes.  */
  182.   init_sig (SIGWINCH, "WINCH", "Window changed");
  183. #endif
  184. #if defined (SIGCONT)
  185.   init_sig (SIGCONT, "CONT", "Continued");
  186. #endif
  187. #if defined (SIGURG)
  188.   init_sig (SIGURG, "URG", "Urgent I/O condition");
  189. #endif
  190. #if defined (SIGIO)
  191.   /* "I/O pending" has also been suggested.  A disadvantage is
  192.      that signal only happens when the process has
  193.      asked for it, not everytime I/O is pending.  Another disadvantage
  194.      is the confusion from giving it a different name than under Unix.  */
  195.   init_sig (SIGIO, "IO", "I/O possible");
  196. #endif
  197. #if defined (SIGWIND)
  198.   init_sig (SIGWIND, "WIND", "SIGWIND");
  199. #endif
  200. #if defined (SIGPHONE)
  201.   init_sig (SIGPHONE, "PHONE", "SIGPHONE");
  202. #endif
  203. #if defined (SIGPOLL)
  204.   init_sig (SIGPOLL, "POLL", "I/O possible");
  205. #endif
  206. #if defined (SIGLOST)
  207.   init_sig (SIGLOST, "LOST", "Resource lost");
  208. #endif
  209. #if defined (SIGDANGER)
  210.   init_sig (SIGDANGER, "DANGER", "Danger signal");
  211. #endif
  212. }
  213.  
  214. /* Return the abbreviation for signal NUMBER.  */
  215.  
  216. char *
  217. sig_abbrev (number)
  218.      int number;
  219. {
  220.   int i;
  221.  
  222.   if (sig_table_nelts == 0)
  223.     signame_init ();
  224.   
  225.   for (i = 0; i < sig_table_nelts; i++)
  226.     if (sig_table[i].number == number)
  227.       return (char *)sig_table[i].abbrev;
  228.   return NULL;
  229. }
  230.  
  231. /* Return the signal number for an ABBREV, or -1 if there is no
  232.    signal by that name.  */
  233.  
  234. int
  235. sig_number (abbrev)
  236.      const char *abbrev;
  237. {
  238.   int i;
  239.  
  240.   if (sig_table_nelts == 0)
  241.     signame_init ();
  242.  
  243.   /* Skip over "SIG" if present.  */
  244.   if (abbrev[0] == 'S' && abbrev[1] == 'I' && abbrev[2] == 'G')
  245.     abbrev += 3;
  246.  
  247.   for (i = 0; i < sig_table_nelts; i++)
  248.     if (abbrev[0] == sig_table[i].abbrev[0]
  249.     && strcmp (abbrev, sig_table[i].abbrev) == 0)
  250.       return sig_table[i].number;
  251.   return -1;
  252. }
  253.  
  254. #ifndef HAVE_SYS_SIGLIST
  255. /* Print to standard error the name of SIGNAL, preceded by MESSAGE and
  256.    a colon, and followed by a newline.  */
  257.  
  258. void
  259. psignal (signal, message)
  260.      int signal;
  261.      const char *message;
  262. {
  263.   if (signal <= 0 || signal >= NSIG)
  264.     fprintf (stderr, "%s: unknown signal", message);
  265.   else
  266.     fprintf (stderr, "%s: %s\n", message, sys_siglist[signal]);
  267. }
  268. #endif
  269.